/**
 * 
 */
package com.hk.Mgr.web.controller.common;

import java.util.List;

import com.hk.commons.vo.ProductInfoRes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hk.Mgr.web.message.CommonReq;
import com.hk.Mgr.web.message.CommonRes;
import com.hk.Mgr.web.message.SessionConstants;
import com.hk.Mgr.web.message.StatusCode;
import com.hk.Mgr.web.message.order.OverseaSafeInfoReq;
import com.hk.Mgr.web.message.order.OverseaSafeInfoRes;
import com.hk.Mgr.web.message.order.ProductInfoReq;
import com.hk.base.BaseCtrl;
import com.hk.base.ResponseBase;
import com.hk.commons.ReqToModelHelper;
import com.hk.commons.entity.product.OverseaSafeInfo;
import com.hk.commons.entity.product.ProductInfo;
import com.hk.service.product.IOverseaSafeInfo;
import com.hk.service.product.IProductInfo;

/**
 * @author psx 公用类
 */
@Controller
@RequestMapping(value = BaseCtrl.CommonRootPath + "/CommonCtrl")
public class CommonCtrl extends BaseCtrl {

	@Autowired
	private IProductInfo productInfoService;
	
	@Autowired
	private IOverseaSafeInfo overseaSafeInfoService;

	/**
	 * @Title: getSecondMenu
	 * @Description: 根据一级菜单返回二级菜单
	 * @param
	 */
	@ResponseBody
	@RequestMapping(value = "/getSecondMenu", method = RequestMethod.POST)
	public ResponseBase<CommonRes> getSecondMenu(
			@RequestBody CommonReq req) {
		ResponseBase<CommonRes> resp = new ResponseBase<CommonRes>();
		String firstMenu=req.getFirstMenu();
		List<String> typeAndFlag=this.getTypeAndFlag();
		String allMenu=SessionConstants.MenuMgr;
		String[] menuArr=allMenu.split(";");
		String selMenu="";
		for(int i=0;i<menuArr.length;i++){
			String idx=menuArr[i].split(":")[0];
			if(idx.equals(firstMenu))
				selMenu=menuArr[i].split(":")[1];
		}
		String[] selMenuArr=selMenu.split(",");
		String retMenu="";
		for(String ts:typeAndFlag){
			String resType=ts.split(";")[0];
			if("2".equals(resType)){
				String resFlag=ts.split(";")[1];
				for(int i=0;i<selMenuArr.length;i++){
					if(selMenuArr[i].equals(resFlag)){
						retMenu=resFlag;
						break;
					}
				}
			}
		}
		
		CommonRes res=new CommonRes();
		res.setSecondMenu(retMenu);
		resp.setResultCode(StatusCode.SUCCESS.key());
		resp.setResult(res);
		return resp;
	}
	
	//根据id获取产品
	/**
	 * @Title: getProductInfoById
	 * @Description: 根据id获取系统模块信息
	 * @param
	 */
	@ResponseBody
	@RequestMapping(value = "/getProductInfoById", method = RequestMethod.POST)
	public ResponseBase<ProductInfoRes> getProductInfoById(
			@RequestBody ProductInfoReq req) {
		ResponseBase<ProductInfoRes> resp = new ResponseBase<ProductInfoRes>();
		try {
			if (req == null || req.getId() == null) {
				resp.setResultCode(StatusCode.PARAM_ERROR.key());
				resp.setMsg("参数不对：id is null！");
				return resp;
			}
			ProductInfo productInfo = productInfoService.findById(req.getId());

			ProductInfoRes singleResp = new ProductInfoRes();
			ReqToModelHelper.copyReqValueToModel(productInfo, singleResp);

			String introduceTxt, incomeTxt, riskctrlTxt = "";
			if (productInfo.getIntroduce() != null) {
				introduceTxt = new String(productInfo.getIntroduce(), "UTF-8");
				singleResp.setIntroduceTxt(introduceTxt);
			}
			if (productInfo.getIncome() != null) {
				incomeTxt = new String(productInfo.getIncome(), "UTF-8");
				singleResp.setIncomeTxt(incomeTxt);
			}
			if (productInfo.getRiskctrl() != null) {
				riskctrlTxt = new String(productInfo.getRiskctrl(), "UTF-8");
				singleResp.setRiskctrlTxt(riskctrlTxt);
			}

			resp.setResultCode(StatusCode.SUCCESS.key());
			resp.setResult(singleResp);
		} catch (Exception ex) {
			logger.error(ex.getMessage(), ex);
			resp.setMsg("操作异常，请重试");
			resp.setResultCode(StatusCode.EXCEPTION.key());
		}
		return resp;
	}
	
	/**
	 * @Title: getOverseaSafeInfoById
	 * @Description: 根据id获取海外产品
	 * @param
	 */
	@ResponseBody
	@RequestMapping(value = "/getOverseaSafeInfoById", method = RequestMethod.POST)
	public ResponseBase<OverseaSafeInfoRes> getOverseaSafeInfoById(
			@RequestBody OverseaSafeInfoReq req) {
		ResponseBase<OverseaSafeInfoRes> resp = new ResponseBase<OverseaSafeInfoRes>();
		try {
			if (req == null || req.getId() == null) {
				resp.setResultCode(StatusCode.PARAM_ERROR.key());
				resp.setMsg("参数不对：id is null！");
				return resp;
			}
			OverseaSafeInfo overseaSafeInfo = overseaSafeInfoService.findById(req.getId());

			OverseaSafeInfoRes singleResp = new OverseaSafeInfoRes();
			ReqToModelHelper.copyReqValueToModel(overseaSafeInfo, singleResp);

			String proFeatureTxt, proEnsureTxt, coverCaseTxt, coverFlowTxt = "";
			if (overseaSafeInfo.getProFeature() != null) {
				proFeatureTxt = new String(overseaSafeInfo.getProFeature(), "UTF-8");
				singleResp.setProFeatureTxt(proFeatureTxt);
			}
			if (overseaSafeInfo.getProEnsure() != null) {
				proEnsureTxt = new String(overseaSafeInfo.getProEnsure(), "UTF-8");
				singleResp.setProEnsureTxt(proEnsureTxt);
			}
			if (overseaSafeInfo.getCoverCase() != null) {
				coverCaseTxt = new String(overseaSafeInfo.getCoverCase(), "UTF-8");
				singleResp.setCoverCaseTxt(coverCaseTxt);
			}
			if (overseaSafeInfo.getCoverFlow() != null) {
				coverFlowTxt = new String(overseaSafeInfo.getCoverFlow(), "UTF-8");
				singleResp.setCoverFlowTxt(coverFlowTxt);
			}

			resp.setResultCode(StatusCode.SUCCESS.key());
			resp.setResult(singleResp);
		} catch (Exception ex) {
			logger.error(ex.getMessage(), ex);
			resp.setMsg("操作异常，请重试");
			resp.setResultCode(StatusCode.EXCEPTION.key());
		}
		return resp;
	}
}
